/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Servlet;

import Entity.Account;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author phuc
 */
public class AccountServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet AccountServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet AccountServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
            String action = request.getParameter("action");
            out.println(action);
            if (action.equals("login"))
            {
                String username = request.getParameter("Username");
                String password = request.getParameter("Password");
                PersistenceManager pm = PMF.get().getPersistenceManager();
                //out.print("Check login");
                try{
                    Query q = pm.newQuery(Account.class,"username == user && password == pass");
                    q.declareParameters("String user,String pass");
                    List<Account> results = (List<Account>)q.execute(username,password);
                    // If Login successful
                    Account acc = results.get(0);
                    if (results.size() > 0)
                    {
                        out.print("User " + acc.getId());
                        HttpSession session = request.getSession(true);
                        session.setAttribute("uid", acc.getId());
                        response.sendRedirect("Default.jsp");
                    }
                    else
                        response.sendRedirect("Login.jsp");
                }catch(Exception e)
                {
                    out.print(e.getMessage());
                }
                finally
                {
                    pm.close();
                }
            }
            else // Create account
            {
                String username = request.getParameter("txtUsername");
                String password = request.getParameter("txtPassword");
                String role = "user";
                PersistenceManager pm = PMF.get().getPersistenceManager();
                try{
                    if (username.trim() != "")
                    {
                        Account acc = new Account(username, password, role);
                        pm.makePersistent(acc);
                        //response.sendRedirect("/Account/DisplayAccount.jsp");
                        out.print("<h1>Dang ky thanh cong</h1>");
                    }
                    else
                        response.sendRedirect("/Account/CreateAccount.jsp");
                    //out.print(username + "/" + password);
                }
                catch(Exception e){
                     out.print(e.getMessage());
                }
                finally
                {
                    pm.close();
                }
            }
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
